package cn.nawang.ebeim.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.domain.StorageFile;
import cn.nawang.ebeim.api.service.StorageFileService;
import cn.nawang.ebeim.api.utils.Constant;

/**
 * @ClassName: StorageFileController
 * @Description: TODO
 * @author fanyb
 * @date Aug 27, 2015 8:38:48 AM
 * @version v1.0
 */
@RestController
public class StorageFileController {

  private static final Logger LOG = LoggerFactory.getLogger(StatisticsController.class);

  @Autowired
  private StorageFileService storageFileService;

  /**
   * 
   * @author fanyb
   * @Description: 新增存储文件对象
   * @param storageFile 存储文件
   * @return Map<String,Object>
   * @date Aug 27, 20158:40:31 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_file/add", method = RequestMethod.GET)
  public Map<String, Object> add(StorageFile storageFile) {
    LOG.debug("/token/storage_file/add {}", storageFile);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    boolean addFlag = storageFileService.add(storageFile);
    if (addFlag) {
      map.put("code", Constant.CODE_SUCCESS);
      return map;
    }
    LOG.debug("add {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据文件签名和数据源编号查找存储文件
   * @param datasourceId 数据源编号
   * @param signature 文件签名
   * @return Map<String,Object>
   * @date Sep 6, 20159:14:13 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_file/find/datasource_id_and_signature", method = RequestMethod.GET)
  public Map<String, Object> findByDatasourceIDAndSignature(String datasourceId, String signature) {
    LOG.debug("/token/storage_file/find/datasource_id_and_signature {}", datasourceId, signature);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    StorageFile storageFile =
        storageFileService.findByDatasourceIDAndSignature(datasourceId, signature);
    map.put("obj", storageFile);
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findByDatasourceIDAndSignature {}", map);
    return map;
  }
}
